package com.ruoyi.gfxmfx.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.gfxmfx.service.lGfxmfxService;
import com.ruoyi.gfxmfx.vo.GfxmfxVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sjfx/gfxmfx")
public class GfxmfxController extends BaseController {

    /**
     * 工法项目分析
     */
    @Resource
    private lGfxmfxService gfxmfxService;

    @GetMapping(value = "/selectGfxmfxList")
    public AjaxResult selectGfxmfxList(GfxmfxVo gfxmfxVo) {
        List<GfxmfxVo> gfxmfxVos = gfxmfxService.selectGfxmfxList();

        // 提取年份并去重
        String[] yearsArray = gfxmfxVos.stream()
                .map(GfxmfxVo::getNd) // 假设getNd()方法可以获取'nd'字段值
                .distinct()           // 去重
                .sorted()             // 可选：排序年份
                .toArray(String[]::new);

        LinkedHashMap<String, Integer> djGfCounts = getGfByCounts("电建工法");
        LinkedHashMap<String, Integer> djJsXhGfCounts = getGfByCounts("电力建设协会工法");
        LinkedHashMap<String, Integer> gjjGfCounts = getGfByCounts("国家级工法");
        LinkedHashMap<String, Integer> qyGfCounts = getGfByCounts("企业工法");
        LinkedHashMap<String, Integer> scsGfCounts = getGfByCounts("四川省工法");
        LinkedHashMap<String, Integer> slJsXhGfCounts = getGfByCounts("水利建设协会工法");
        LinkedHashMap<String, Integer> hnsGfCounts = getGfByCounts("河南省工法");

        int[] djGfCountsArray = encapsulateValuesToPrimitiveArray(djGfCounts);
        int[] djJsXhGfCountsArray = encapsulateValuesToPrimitiveArray(djJsXhGfCounts);
        int[] gjjGfCountsArray = encapsulateValuesToPrimitiveArray(gjjGfCounts);
        int[] qyGfCountsArray = encapsulateValuesToPrimitiveArray(qyGfCounts);
        int[] scsGfCountsArray = encapsulateValuesToPrimitiveArray(scsGfCounts);
        int[] slJsXhGfCountsArray = encapsulateValuesToPrimitiveArray(slJsXhGfCounts);
        int[] hnsGfCountsArray = encapsulateValuesToPrimitiveArray(hnsGfCounts);

        // 使用 Map 封装数据
        Map<String, Object> result = new HashMap<>();
        result.put("yearsArray", yearsArray);
        result.put("djGfCountsArray", djGfCountsArray);
        result.put("djJsXhGfCountsArray", djJsXhGfCountsArray);
        result.put("gjjGfCountsArray", gjjGfCountsArray);
        result.put("qyGfCountsArray", qyGfCountsArray);
        result.put("scsGfCountsArray", scsGfCountsArray);
        result.put("slJsXhGfCountsArray", slJsXhGfCountsArray);
        result.put("hnsGfCountsArray", hnsGfCountsArray);

        return success(result);
    }

    private LinkedHashMap<String, Integer> getGfByCounts(String gfJb) {

        List<GfxmfxVo> gfxmfxVos = gfxmfxService.selectGfxmfxList();

        return gfxmfxVos.stream()
                .filter(item -> gfJb.equals(item.getGfJb()))
                .collect(Collectors.groupingBy(
                        GfxmfxVo::getNd,
                        TreeMap::new, // 使用TreeMap自动按键排序
                        Collectors.summingInt(GfxmfxVo::getSl)
                ))
                .entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new // 转换为LinkedHashMap以保留排序结果
                ));
    }

    public int[] encapsulateValuesToPrimitiveArray(LinkedHashMap<String, Integer> counts) {
        return counts.values().stream().mapToInt(Integer::intValue).toArray();
    }

}
